package com.boco.fznq.home;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.boco.fznq.R;
import com.boco.fznq.main.MainActivity;
import com.boco.fznq.model.BannerModel;
import com.boco.fznq.webview.MyWebviewActivity;
import com.culiu.core.fonts.CustomTextView;
import com.culiu.core.utils.notification.ToastUtils;
import com.culiu.core.widget.CustomImageView;

import java.io.InputStream;
import java.util.List;

/**
 * Created by jiangcheng on 2016/12/13.
 */

public class HomeGridAdapter extends BaseAdapter {


    private Context mContext;

    private List<BannerModel> mDatas;

    public HomeGridAdapter(Context context, List<BannerModel> datas) {
        this.mContext = context;
        this.mDatas = datas;
    }

    @Override
    public int getCount() {
        return mDatas.isEmpty() ? 0 : mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_home_icon, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final BannerModel bannerModel = mDatas.get(position);

        if (bannerModel != null) {
            viewHolder.ctv_title.setText(bannerModel.getTitle());
//            CuliuImageLoader.getInstance().display(viewHolder.civ_icon, bannerModel.getIconUrl(), R.drawable.icon_search_taobao);
            viewHolder.civ_icon.setImageBitmap(readBitMap(mContext, bannerModel.getResId()));
        }



        convertView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                switch (position) {
                    case 0:
                        if (mContext instanceof MainActivity) {
                            MainActivity activity = (MainActivity) mContext;
                            activity.showClassifyFragment();
                        }
                        break;
                    case 1:
                    case 2:
                        ToastUtils.showShort((Activity) mContext, "该功能暂未开放");
                        break;
                    case 3:
                        MyWebviewActivity.startMyWebView((Activity) mContext,
                                "http://www.epicc.com.cn/m/");
                        break;
                }
            }
        });
        return convertView;
    }

    static class ViewHolder {
        CustomImageView civ_icon;
        CustomTextView ctv_title;

        public ViewHolder(View view) {
            civ_icon = (CustomImageView) view.findViewById(R.id.civ_icon);
            ctv_title = (CustomTextView) view.findViewById(R.id.ctv_title);
        }
    }


    /**
     * 以最省内存的方式读取本地资源的图片
     * @param context
     * @param resId
     * @return
     */
    public static Bitmap readBitMap(Context context, int resId)
    {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;//获取资源图片
        InputStream is = context.getResources().openRawResource(resId);
        return BitmapFactory.decodeStream(is, null, opt);
    }


}
